/**
 * 
 */
package com.jm.web.tags;

import com.jm.utils.StringHelper;
import com.jm.web.builder.TabBuilder;
import com.jm.web.form.DPanel;
import com.jm.web.form.Iframe;
import com.jm.web.form.TabItem;
import com.jm.web.form.support.FormItem;
import com.jm.web.tags.support.DataSourceTag;

/**
 * @author yanjie
 *
 * 2011/9/19 上午10:39:09
 * 标签页控件
 */
public class TabTag extends DataSourceTag{
	private static final long serialVersionUID = -6894281604635304860L;
	protected BaseDPTag parentTag = null;
	protected TabItem  tab = null;
	
	public TabItem getTab() {
		return tab;
	}

	public void setTab(TabItem tab) {
		this.tab = tab;
	}

	@Override
	public String getBuilderName() {
		return TabBuilder.class.getName();
	}
	
	@Override
	protected void preStartTag() {
		super.preStartTag();
		parentTag = (BaseDPTag)getParent();
		
		if(!StringHelper.IsNullOrEmpty(parentTag.getData())){
			this.setData(parentTag.getData());
			this.setScope(parentTag.getScope());
		}
	}
	
	/* 
	 * 不执行标签内部的内容
	 */
	@Override
	protected int onStartTag() {
		if(this.getPage().isExport() && !this.isEp()){
			return SKIP_BODY;
		}
		
		//搜索表单只能有两个标签页,超过两个则不显示
		if(this.parentTag.getTabSize() > 2 )
			return SKIP_BODY;
		
		if(this.GetExtValue("keep", false))
			return super.onStartTag();
		
		return EVAL_BODY_BUFFERED;
	}
	
	/*
	 * 加载每一个标标签页的配置
	 */
	@Override
	protected void preEndTag() {
		//父标签的当前tab index 加1
		parentTag.addCurrentTabIndex();
		
		//加载标签页的配置
		String strXml = "";
		if(this.bodyContent != null)
			strXml = StringHelper.getValue(this.bodyContent.getString()).trim();
		
		tab = new TabItem();
		tab.InitTag(this);
		if(StringHelper.IsNullOrEmpty(this.getId()))
			tab.setId(StringHelper.Format("%1$s-%2$s",parentTag.getId(),parentTag.getCurrentTabIndex()));
		else
			tab.setId(this.getId());
		
		tab.SetExtValue("tabid", parentTag.getId()+"_tab"+parentTag.getCurrentTabIndex());
		
		String strSrc = this.GetExtValue("src","");
		if(StringHelper.IsNullOrEmpty(strSrc)){
			if(!this.GetExtValue("keep",false)){
				DPanel form = new DPanel();
				form.InitTag(this);
				if(strXml.startsWith("<dp"))
					FormItem.LoadFromXML(strXml,form);
				else{
//					if(this.GetExtValue("keep",false)){
//						FormItem.LoadFromXML(StringHelper.Format("<dp colModel=\"%1$s\"><keep>%2$s</keep></dp>",this.GetExtValue("colModel",""),strXml)
//								,form);
//					}else{
//						FormItem.LoadFromXML(StringHelper.Format("<dp colModel=\"%1$s\">%2$s</dp>",this.GetExtValue("colModel",""),strXml)
//								,form);
//					}
					
					FormItem.LoadFromXML(StringHelper.Format("<dp colModel=\"%1$s\">%2$s</dp>",this.GetExtValue("colModel",""),strXml)
							,form);
				}
				form.init();
				this.prepareParams(form);
				tab.setForm(form);
			}
		}else{
			Iframe iframe = new Iframe();
			iframe.InitTag(this);
			iframe.init();
			this.prepareParams(iframe);
			
			if(this.GetExtValue("delay", false)){
				iframe.setSrc("about:blank");
				iframe.setRsrc(strSrc);
			}else{
				iframe.setSrc(strSrc);
			}
			tab.setIframe(iframe);
		}
	}
	
	
	public void setKeep(String strData) {
		this.SetExtValue("keep", strData);
	}
	
	public void setSrc(String strData) {
		this.SetExtValue("src", strData);
	}
	
	public void setHeight(String strData) {
		this.SetExtValue("height", strData);
	}
	
	public void setWidth(String strData) {
		this.SetExtValue("width", strData);
	}
	
	public void setAlign(String strData) {
		this.SetExtValue("align", strData);
	}
	
	public void setLayout(String strValue) {
		this.SetExtValue("layout",strValue);
	}
	
	public void setDelay(String strValue) {
		this.SetExtValue("delay",strValue);
	}
	
	public void setScrolling(String strValue) {
		this.SetExtValue("scrolling",strValue);
	}
	
	@Override
	public void clear() {
		parentTag = null;
		tab = null;
		super.clear();
	}
	
	public void setColModel(String strValue) {
		this.SetExtValue("colModel", strValue);
	}
}
